<?php


namespace Biz\service\brand;


use Biz\model\brand\BrandCollege;
use Doraemon\service\Photos;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;
use Doraemon\pockets\constInterface\FormatScene;
use Doraemon\service\Format\Note\HandleNote;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\model\Photos as mPhotos;

class BrandCollegeService
{
    #运营号UID
    const OP_UID = 12893279;

    #能设置在品牌商学院的NOTE数量
    const MAX_SET_NOTE_COUNT = 20;

    #客户端显示的最多数量
    const MAX_CLIENT_SHOW_COUNT = 6;

    /**
     * @desc 获取品牌商学院的NOTE列表
     * @return array|bool
     */
    public static function getList()
    {
        $result = BrandCollege::getList();
        $list = [];
        if ($result && is_array($result)) {
            $list = $result;
        }
        return $list;
    }

    /**
     * @desc 品牌商学院新增NOTE
     *
     * @param $objId
     *
     * @return bool
     */
    public static function insterObj($objId)
    {
        return BrandCollege::insterObj($objId);
    }

    /**
     * @desc 品牌商学院修改已设置的NOTE
     *
     * @param $id
     * @param $objId
     *
     * @return bool
     */
    public static function updateObj($id, $objId)
    {
        return BrandCollege::updateObj($id, $objId);
    }

    /**
     * @desc 品牌商学院删除设置的NOTE
     *
     * @param $id
     *
     * @return bool
     */
    public static function deleteObj($id)
    {
        return BrandCollege::deleteObj($id);
    }

    /**
     * @desc 品牌商学院NOTE排序
     *
     * @param $sortArr
     *
     * @return bool
     */
    public static function sortObj($sortArr)
    {
        return BrandCollege::weight($sortArr);
    }

    /**
     * @desc 品牌商学院NOTE数量
     * @return int
     */
    public static function objCount()
    {
        return (int)BrandCollege::getObjsCount();
    }

    /**
     * @desc NOTE是否已存在
     *
     * @param $objId
     *
     * @return bool
     */
    public static function objExsit($objId)
    {
        $obj = BrandCollege::getObj($objId);
        if ($obj && is_array($obj) && !empty($obj)) {
            return true;
        }
        return false;
    }

    /**
     * @desc 品牌商学院NOTE展示类型
     *
     * @param $showType
     *
     * @return string
     */
    public static function showType($showType)
    {
        $showTypeMap = [
            mPhotos::SHOW_TYPE_PHOTO => '图片',
            mPhotos::SHOW_TYPE_VIDEO => '视频',
        ];
        return $showTypeMap[$showType] ?? '';
    }

    /**
     * @desc 品牌商学院NOTE状态
     *
     * @param $status
     *
     * @return string
     */
    public static function getStatus($status)
    {
        if(in_array($status,mPhotos::BASE_VISIBLE_STATUS)){
            return '正常';
        }
        return '已删除';
    }

    /**
     * @desc  品牌商学院新增NOTE时objId验证
     *
     * @param $objId
     *
     * @return bool
     */
    public static function objIdCheck($objId)
    {
        $type = IdParse::getType($objId);
        if (BlockType::TYPE_PHOTO != $type) {
            return false;
        }
        $uid = IdParse::getUidFromObjId($objId);
        if (self::OP_UID != $uid) {
            return false;
        }
        $info = Photos::getPhoto($objId);
        if (empty($info)) {
            return false;
        }
        if (BrandCollege::STATUS_DELETE == $info['status']) {
            return false;
        }
        if (!in_array($info['show_type'],
            [mPhotos::SHOW_TYPE_PHOTO, mPhotos::SHOW_TYPE_VIDEO])
        ) {
            return false;
        }

        return true;
    }

    /**
     * @获取app个人主页内链
     *
     * @param $uid
     *
     * @return string
     */
    public static function getMoreLink($uid)
    {
        return 'niceliving://user/detail?protego='
            .urlencode(base64_encode(json_encode(['uid' => $uid])));
    }

    /**
     * @desc 获取品牌商学院NOTES
     * @return array
     */
    public static function getCollegeNotes()
    {
        $noteIds = BrandCollege::getObjIds(self::MAX_SET_NOTE_COUNT);
        $list = [];
        if ($noteIds) {
            $list = HandleNote::handleNote($noteIds,
                FormatScene::MASONRY_LAYOUTS, self::OP_UID);
            $index = 1;
            $list = array_slice($list,0,self::MAX_CLIENT_SHOW_COUNT,true);
            foreach ($list as $objId => $photoInfo) {
                $list[$objId]['statSign']
                    = basicStatSign::genStatSign(basicStatSign::MODULE_BRAND_HOME_SLIDER,
                    [
                        basicStatSign::EXTEND_KEY_OBJ_ID   => $objId,
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => IdParse::getType($objId),
                        basicStatSign::EXTEND_KEY_OBJ_UID  => IdParse::getUid($objId),
                        basicStatSign::EXTEND_KEY_INDEX    => $index++,
                    ]);
            }
        }
        $result = [
            'list'          => count($list) >= 3 ? array_values($list) : [],
            'view_all_link' => self::getMoreLink(strval(self::OP_UID)),
        ];
        return $result;
    }

}